package urlBase.impl;

import urlBase.URLBase;

import java.util.HashSet;
import java.util.concurrent.LinkedBlockingQueue;

public class LocalBase implements URLBase {

    private LocalBase(){}
    private static LocalBase instance = new LocalBase();
    public static LocalBase getURLBase() {
        return instance;
    }

    public LinkedBlockingQueue<String> index = new LinkedBlockingQueue<String>();
    public LinkedBlockingQueue<String> item = new LinkedBlockingQueue<String>();
    public HashSet<String> trash = new HashSet<String>();

    public void addIndex(String url) {
        try {
            index.put(url);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void addItem(String url) {
        try {
            item.put(url);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String getIndex()
    {
        String url = null;
        try {
            url = index.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return url;
    }

    public String getItem()
    {
        String url = null;
        try {
            url = item.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return url;
    }

    public void discard(String url)
    {
        trash.add(url);
    }

    public boolean exist(String url)
    {
        return trash.contains(url);
    }

    public long size()
    {
        return item.size();
    }

}
